package com.suneee.shop.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.suneee.base.util.ContextHelper;
import com.suneee.context.page.Page;
import com.suneee.context.page.Sort;
import com.suneee.context.page.Sort.Order;
import com.suneee.core.util.StringUtils;
import com.suneee.orm.GenericDao;
import com.suneee.orm.util.WhereClauseBuilder;
import com.suneee.shop.bean.ShopSearchBean;
import com.suneee.shop.dao.ShopDao;
import com.suneee.shop.entity.Shop;

@Repository("shopDao")
public class ShopDaoImpl extends GenericDao<Shop, Integer>implements ShopDao {

	@Override
	public boolean existsName(String name) {
		Object[] arg = new Object[2];
		arg[0] = name;
		arg[1] = ContextHelper.getCurrentLoginInfo().getEnterprise().getEnterpriseCode();
		;
		List<Shop> list = this.selectWhere("name = ? and enterprise_code=? ", arg);
		return list != null && list.size() > 0;
	}

	/**
	 * 判断是否重复
	 * 
	 * @param name
	 * @param value
	 * @return
	 */
	private boolean exists(Object name, Object value) {
		List<Shop> list = this.selectWhere(name + " = ? ", value);
		return list != null && list.size() > 0;
	}

	@Override
	public boolean existsId(int id) {
		return this.exists("shop_id", id);
	}

	/**
	 * 获取单条行业分类信息
	 * 
	 * @param shopId
	 * @return
	 */
	@Override
	public Shop get(int shopId) {
		return this.select(shopId);
	}

	@Override
	public void updateShop(Shop shop) {
		StringBuffer sql = new StringBuffer();
		sql.append("update t_shop set ");
		sql.append(" name=? ");
		sql.append(" , remark=? ");
		sql.append(" , industry_id=? ");
		sql.append(" , channel_code=? ");
		sql.append(" where shop_id=? ");
		Object[] arg = new Object[5];
		arg[0] = shop.getName();
		arg[1] = shop.getRemark();
		arg[2] = shop.getIndustryId();
		arg[3] = shop.getChannelCode();
		arg[4] = shop.getShopId();
		this.executeUpdate(sql.toString(), arg);

	}

	@Override
	public void deleteByShopId(int shopId) {
		this.deleteWhere(" shop_id=?", shopId);
	}

	@Override
	public Page<Map<String, Object>> findShop(ShopSearchBean bean) 
	{
		String sysSource = ContextHelper.getCurrentLoginInfo().getEnterprise().getType();
		Object [] args = new Object[1];
		args[0] = ContextHelper.getCurrentLoginInfo().getAdminId();
		List<Map<String,Object>> spadmList = this.queryForList("select shop_id from t_shop_admin where admin_id = ?", args);
		List<Object> objList = null;
		if(null != spadmList && spadmList.size() > 0)
		{
			objList = new ArrayList<Object>();	
			for(Map<String,Object> map :spadmList)
			{
				objList.add(map.get("shop_id"));
			}
		}
		if(null == spadmList || spadmList.size() == 0)
		{
			objList = new ArrayList<Object>();
			objList.add(" ");
		}
		String sql = "SELECT s.shop_id AS shopId,s.industry_id AS industryId,s.enterprise_code AS enterpriseCode,s.channel_code AS channelCode,s.name,s.remark,s.status,s.enabled,s.rule_info AS ruleInfo,s.proxy_url AS proxyURL,s.create_time AS createTime,s.create_user AS createUser,c.name AS channelName,c.rule_json AS ruleJson FROM t_shop s LEFT JOIN t_channel c ON  s.channel_code = c.channel_code  WHERE 1 = 1 ";
		WhereClauseBuilder builder = new WhereClauseBuilder(sql);
		builder.equals("s.enterprise_code", bean.getEnterpriseCode());
		builder.equals("s.channel_code", bean.getChannelCode());
		builder.contains("s.name", bean.getName());
		builder.noEquals("s.status", bean.getStatus());
		if("client".equals(sysSource))
		{
			builder.in("s.shop_id", objList);	
		}
		bean.setPageNumber(bean.getPageNumber() == 0 ? 1 : bean.getPageNumber());
		bean.setPageSize(bean.getPageSize() == 0 ? 10 : bean.getPageSize());
		bean.setSort((bean.getSort() == null || StringUtils.isEmpty(bean.getSort().getOrderByClause()))
				? new Sort("shop_id", Order.ASC) : bean.getSort());
		return this.queryForList(builder.getSql(), bean, builder.getArgs());
	}

	@Override
	public void updateEnabled(int shopId, boolean status) {
		StringBuffer sql = new StringBuffer();
		sql.append("update t_shop set ");
		sql.append(" enabled=? ");
		sql.append(" where shop_id=? ");
		Object[] arg = new Object[2];
		arg[0] = status;
		arg[1] = shopId;
		this.executeUpdate(sql.toString(), arg);
	}

	@Override
	public void updateRuleInfo(int shopId, String ruleInfo, String status) {
		StringBuffer sql = new StringBuffer();
		sql.append("update t_shop set ");
		sql.append(" rule_info=? ,");
		sql.append(" status=? ");
		sql.append(" where shop_id=? ");
		Object[] arg = new Object[3];
		arg[0] = ruleInfo;
		arg[1] = status;
		arg[2] = shopId;
		this.executeUpdate(sql.toString(), arg);
	}

	@Override
	public void updateProxyURL(int shopId, String proxyURL, String status) {
		StringBuffer sql = new StringBuffer();
		sql.append("update t_shop set ");
		sql.append(" proxy_url=? ,");
		sql.append(" status=? ");
		sql.append(" where shop_id=? ");
		Object[] arg = new Object[3];
		arg[0] = proxyURL;
		arg[1] = status;
		arg[2] = shopId;
		this.executeUpdate(sql.toString(), arg);
	} 

	@Override
	public List<Shop> listEnterpriseShop(String enterpriseCode) 
	{
		Object [] arg = new Object[3];
		arg[0] = enterpriseCode;
		arg[1] = "use";
		arg[2] = ContextHelper.getCurrentLoginInfo().getAdminId(); 
		return this.selectWhere(" enterprise_code = ? and status = ? and enabled = 1 and shop_id in (select shop_id from t_shop_admin where admin_id = ?) ", arg);
	}

	@Override
	public List<Shop> shopByShopName(String name) {
		String enterpriseCode = ContextHelper.getCurrentLoginInfo().getEnterprise().getEnterpriseCode();
		Object[] arg = new Object[2];
		arg[0] = name;
		arg[1] = enterpriseCode;

		return this.selectWhere(" name = ? and enterprise_code=?", arg);
	}

	@Override
	public List<Shop> toBeButtShopList(String enterpriseCode) {
		WhereClauseBuilder sbf = new WhereClauseBuilder();
		sbf.equals("enterprise_code",enterpriseCode);
		sbf.equals("status", "create");
        sbf.appendWhere("shop_id IN (SELECT shop_id FROM t_shop_admin WHERE admin_id ="+ ContextHelper.getCurrentLoginInfo().getAdminId()+") ");
		return this.selectWhere(sbf.getSql(),sbf.getArgs());
	}
}
